package net.tv90.bfmccore.code;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import net.tv90.bfmccore.BFMCCore;

import java.io.File;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;

public class VehicleMenuAssets {

    public static Map<String, VehicleMenu> vehicles = new LinkedHashMap<>();

    public static Map<String, VehicleGroupParam> vehicleGroupMap = new HashMap<>();

    private static void initVehicleGroup() {
        File vehicleGroupFile = new File(BFMCCore.PLUGIN_ROOT, "vehicle_group/vehicle_group.json");
        if (!vehicleGroupFile.exists()) {
            vehicleGroupMap.clear();
            return;
        }
        try (Reader reader = new InputStreamReader(Files.newInputStream(vehicleGroupFile.toPath()), StandardCharsets.UTF_8)) {
            Gson gson = new Gson();
            Type listType = new TypeToken<List<VehicleGroupParam>>() {
            }.getType();
            List<VehicleGroupParam> params = gson.fromJson(reader, listType);
            vehicleGroupMap.clear();
            if (params != null) {
                for (VehicleGroupParam p : params) {
                    vehicleGroupMap.put(p.name, p);
                }
            }
            System.out.println("加载vehicle_group.json成功，共" + vehicleGroupMap.size() + "个载具组");
        } catch (Exception e) {
            System.out.println("读取vehicle_group.json出错");
            e.printStackTrace();
            vehicleGroupMap.clear();
        }
    }

    /**
     * 将 JSON 文件内容解析为 VehicleMenuMap
     */
    public static void initVehicleDetails() {
        File file = new File(BFMCCore.PLUGIN_ROOT, "vehicle_group/vehicle_details.json");
        if (!file.exists()) {
            vehicles.clear();
            return;
        }
        vehicles.clear();
        try (Reader reader = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8)) {
            Gson gson = new Gson();
            VehicleMenuJson jsonData = gson.fromJson(reader, VehicleMenuJson.class);
            if (jsonData != null && jsonData.getVehicles() != null) {
                for (VehicleMenuItem item : jsonData.getVehicles()) {
                    VehicleMenu menu = new VehicleMenu(item.getName(), item.getDescription(), item.getId());
                    vehicles.put(item.getKey(), menu);
                }
            }
            System.out.println("加载vehicle_details.json成功，共" + vehicles.size() + "个载具详情");
        } catch (Exception e) {
            System.out.println("读取vehicle_details.json出错");
            e.printStackTrace();
        }
    }

    public static List<String> resolveVehicleGroup(List<String> group) {
        List<String> result = new ArrayList<>();
        if (group != null && !group.isEmpty()) {
            for (String s : group) {
                if (s.startsWith("_")) {
                    VehicleGroupParam param = VehicleMenuAssets.vehicleGroupMap.get(s);
                    if (param != null && param.list != null) {
                        result.addAll(param.list);
                    }
                } else {
                    result.add(s);
                }
            }
        }
        return result;
    }

    public static void init() {
        initVehicleGroup();
        initVehicleDetails();
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class VehicleMenuItem implements Serializable {
        private String key;           // 载具在原始 map 中的 key
        private String name;          // 显示名称
        private List<String> description; // 描述文本列表
        private String id;            // 载具的内部标识
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class VehicleMenuJson implements Serializable {
        private List<VehicleMenuItem> vehicles;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    static class VehicleGroupParam implements Serializable {
        public String name;
        public List<String> list;
    }

}
